<template>
  <div class="mod-demo-echarts">
    <el-row :gutter="20">
      <el-col :span="12">
        <el-card>
          <div id="J_ramchartLineBox" class="chart-box"></div>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card>
          <div id="J_chartBarBox" class="chart-box"></div>
        </el-card>
      </el-col>
      <el-col :span="24">
    <el-table
      :data="dataList"
      border
      style="width: 100%;">
      <el-table-column
        prop="name"
        header-align="left"
        align="left"
        width="250"
        :show-overflow-tooltip="true"
        label="参数">
      </el-table-column>
      <el-table-column
        prop="des"
        header-align="left"
        align="left"
        :show-overflow-tooltip="true"
        label="说明">
      </el-table-column>
      <el-table-column
        prop="value"
        header-align="left"
        align="left"
        width="300"
        :show-overflow-tooltip="true"
        label="值">
      </el-table-column>
    </el-table>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import echarts from 'echarts'
  export default {
    data () {
      return {
        dataList: [],
        ramchartLine: null,
        keychartLine: null,
        usedramdata: [],
        peakramdata: [],
        keysNumdata: [],
        timer: ''
      }
    },
    mounted () {
      this.initramchartLine()
      this.initkeychartLine()
    },
    activated () {
      // 由于给echart添加了resize事件, 在组件激活时需要重新resize绘画一次, 否则出现空白bug
      if (this.ramchartLine) {
        this.ramchartLine.resize()
      }
      if (this.keychartLine) {
        this.keychartLine.resize()
      }
      this.timer = setInterval(this.refreshData, 2000)
    },
    beforeDestroy () {
    },
    deactivated () {
      clearInterval(this.timer)
    },
    methods: {
      // 折线图
      initramchartLine () {
        var option = {
          'title': {
            'text': 'Redis 内存使用情况'
          },
          'tooltip': {
            'trigger': 'axis'
          },
          'grid': {
            'left': '3%',
            'right': '4%',
            'bottom': '3%',
            'containLabel': true
          },
          'toolbox': {
            'feature': {
              'saveAsImage': { }
            }
          },
          'xAxis': {
            'type': 'time',
            splitNumber: 8,
            splitLine: {
              show: false
            },
            axisLabel: {
              formatter: function (value) { // 在这里写你需要的时间格式
                var tdate = new Date(value)
                return [tdate.getHours(), tdate.getMinutes(), tdate.getSeconds()].join(':')
              }
            }
          },
          'yAxis': {
            'type': 'value',
            axisLabel: {
              formatter: '{value} M'// 给Y轴上的刻度加上单位
            }
          },
          'series': [
            {
              'name': '消耗峰值',
              'type': 'line',
              data: this.peakramdata
            },
            {
              'name': '分配内存',
              'type': 'line',
              data: this.usedramdata
            }
          ]
        }
        this.ramchartLine = echarts.init(document.getElementById('J_ramchartLineBox'))
        this.ramchartLine.setOption(option)
        window.addEventListener('resize', () => {
          this.ramchartLine.resize()
        })
      },
      initkeychartLine () {
        var option = {
          'title': {
            'text': 'Redis Key数量'
          },
          'tooltip': {
            'trigger': 'axis'
          },
          'grid': {
            'left': '3%',
            'right': '4%',
            'bottom': '3%',
            'containLabel': true
          },
          'toolbox': {
            'feature': {
              'saveAsImage': { }
            }
          },
          'xAxis': {
            'type': 'time',
            splitNumber: 8,
            splitLine: {
              show: false
            },
            axisLabel: {
              formatter: function (value) { // 在这里写你需要的时间格式
                var tdate = new Date(value)
                return [tdate.getHours(), tdate.getMinutes(), tdate.getSeconds()].join(':')
              }
            }
          },
          'yAxis': {
            'type': 'value',
            axisLabel: {
              formatter: '{value} 个'// 给Y轴上的刻度加上单位
            }
          },
          'series': [
            {
              'name': 'Key数量',
              'type': 'line',
              data: this.keysNumdata
            }
          ]
        }
        this.keychartLine = echarts.init(document.getElementById('J_chartBarBox'))
        this.keychartLine.setOption(option)
        window.addEventListener('resize', () => {
          this.keychartLine.resize()
        })
      },
      refreshData () {
        this.$http({
          url: this.$http.adornUrl('/monitor/redis'),
          method: 'get',
          params: this.$http.adornParams({})
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.all
            if (this.usedramdata.length >= 8) this.usedramdata.shift()
            if (this.peakramdata.length >= 8) this.peakramdata.shift()
            if (this.keysNumdata.length >= 8) this.keysNumdata.shift()
            this.usedramdata.push({ value: [new Date(), data.data.usedMemory] })
            this.peakramdata.push({ value: [new Date(), data.data.usedMemoryPeak] })
            this.keysNumdata.push({ value: [new Date(), data.data.keysNum] })
            this.ramchartLine.setOption({
              series: [{ data: this.peakramdata },
                { data: this.usedramdata }
              ]
            })
            this.keychartLine.setOption({
              series: [{ data: this.keysNumdata }
              ]
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  .mod-demo-echarts {
    > .el-alert {
      margin-bottom: 10px;
    }
    > .el-row {
      margin-top: -10px;
      margin-bottom: -10px;
      .el-col {
        padding-top: 10px;
        padding-bottom: 10px;
      }
    }
    .chart-box {
      min-height: 400px;
    }
  }
</style>
